package com.alt.models;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import com.alt.manager.ALTPTCMgr;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;

public class AccessDeviceInfo {

	public static String getDeviceOS() {
		return android.os.Build.VERSION.RELEASE;
	}

	public static String getPhonenumber(Context context) {
		TelephonyManager phoneManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String phoneNumber = phoneManager.getLine1Number();
		if (phoneNumber != null && !phoneNumber.isEmpty()) {
			if (phoneNumber.startsWith("+82")) {
				phoneNumber = phoneNumber.substring(3);
			}
			
			if (!phoneNumber.startsWith("0")) {
				phoneNumber = "0" + phoneNumber;
			}			
		} else {
			phoneNumber =  ALTPTCMgr.PHONE_NUMBER_NA;
		}
	
		return phoneNumber;
	}

	public static String getDeviceID(Context context) {
		return Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
	}

	public static String getDeviceBrandName() {
		if (android.os.Build.MODEL != null) {
			return java.net.URLEncoder.encode(android.os.Build.MODEL);
		}
		return "empty";
	}

	public static String getDeviceMAC(Context context) {
		WifiManager wifiMan = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInf = wifiMan.getConnectionInfo();
		return wifiInf.getMacAddress();
	}

	@SuppressLint("SimpleDateFormat")
	public static String getLocalTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH-mm-ss");
		return sdf.format(new Date());
	}

	public static String getDeviceIpAddress() throws SocketException {

		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "empty";
	}


}
